/**
 * Copyright (c) 2019, ShangHai YTO Co., Ltd.
 * All right reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF YTO
 * CO., LTD.  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED
 * TO THIRD PARTIES, COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 * WITHOUT THE PRIOR WRITTEN PERMISSION OF YTO
 * CO., LTD.
 */
package org.sc.xl.algorithm.easyexcel226;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.google.common.collect.Lists;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;

import java.util.List;

/**
 * @description: TODO 请在此添加描述
 * @reason: TODO ADD REASON(可选)
 * @author FXL
 *
 * @date 2024/10/19 11:37 
 * @since JDK 1.8
 */
public class EasyExcel226 implements CellWriteHandler {


    public static void main(String[] args) {
        EasyExcel.write("/Users/fxl/Downloads/a_aa.xlsx", Aaa.class).sheet("模板").registerWriteHandler(new EasyExcel226()).doWrite(Lists.newArrayList());
    }


    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {

    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean aBoolean) {

    }

    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer integer, Boolean aBoolean) {

    }


    private static final String DEFAULT_BACKGROUND_COLOR = "#ddebf7";

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
        SXSSFWorkbook workbook = (SXSSFWorkbook) cell.getSheet().getWorkbook();
        CellStyle xssfCellStyle = workbook.createCellStyle();
        // 设置背景颜色
//        xssfCellStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(148, 0, 211)));
        int r = Integer.parseInt((DEFAULT_BACKGROUND_COLOR.substring(1,3)),16);
        int g = Integer.parseInt((DEFAULT_BACKGROUND_COLOR.substring(3,5)),16);
        int b = Integer.parseInt((DEFAULT_BACKGROUND_COLOR.substring(5,7)),16);
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFPalette palette = wb.getCustomPalette();
        HSSFColor hssfColor = palette.findSimilarColor(r,g,b);

        XSSFColor color = new XSSFColor();
        xssfCellStyle.setFillBackgroundColor();

        xssfCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        // 重点！！！ 由于在FillStyleCellWriteHandler，会把OriginCellStyle和WriteCellStyle合并，会已WriteCellStyle样式为主，所有必须把WriteCellStyle设置的背景色清空
        cell.setCellStyle(xssfCellStyle);


    }
}
